var formName = "#sniperForm";
var backData;
$().ready(function () {

    var options = {
        beforeSubmit: showRequest,
        success: showResponse,
        type: 'post',
        dataType: 'json',
        clearForm: false,
        timeout: 4000
    };

//        $(formName).submit(function () {
//            $(this).ajaxSubmit(options);
//            return false;
//        });


    $(formName).on('submit', function (e) {

        e.preventDefault();
        $(this).ajaxSubmit(options);

    });


});


$(formName).validationEngine('attach', {
    promptPosition: "topLeft",
    showOneMessage: true,
    maxErrorsPerField: 1
});

// pre-submit callback
function showRequest(formData, jqForm, options) {
    // $('#id').validationEngine('validate'));

    var validate = $(formName).validationEngine('validate');
    if (validate) {
        if (confirm("提交数据?")) {
            layer.msg("稍等...");
            return true;
        }
    }
    return false;
}

//var failCallBack;
//var successCallBack;
// post-submit callback
function showResponse(responseText, statusText, xhr, $form) {
    backData = responseText;
    //关闭所有窗口
    if (statusText === 'success') {
        id = 0;
        var html = responseText.msg;
        html += "\n";

        if (responseText.code === 500) {
            for (var i in responseText.errors) {
                html += responseText.errors[i] + "\n";
            }
        }
        //执行失败回调
        if (responseText.failCallBack) {
            failCallBack = responseText.failCallBack;
            //failCallBack()();
            //window[failCallBack](responseText);
            eval(failCallBack + "()");
        }
        //执行成功回调
        if (responseText.successCallBack) {
            successCallBack = responseText.successCallBack;
            eval(successCallBack + "()");
        }
        //设置当前sid的值
        layer.msg(html);
        //$(document).scrollTop(0);
        if (responseText.code === 200) {
            if (responseText.backUrl) {
                setTimeout("window.location.href=\"" + responseText.backUrl + "\"", 1000);
            } else {
                //setTimeout("window.location.reload(false)", 2000);
            }
        } else {
            $(document).scrollTop(0);
        }

    } else {
        layer.msg("未知错误");
    }

}